var devmod = Class.create ({
	
	initialize: function(devmod_sql_table, checksum, callback_url, devmod_container, devmod_loader) {
	
		this.devmod_sql_table = devmod_sql_table;
	
		this.checksum = checksum;
		
		this.callback_url = callback_url;
		
		this.devmod_container = devmod_container;
		
		this.devmod_loader = devmod_loader;
		
		this.layer_id = 'layer';
		
		this.timeout = null;
		
		this.active_tab = null;
		
		this.start();
	},
	
	start : function() {

		new Ajax.Request(this.returnUrl('list_ajax'), {
			
			onSuccess : function(transport) {
				$(this.devmod_container).update(transport.responseText);
			}.bind(this)
		
		});
		
	},
	
	/* add method */
	
	add : function() {
		
		new Ajax.Request(this.returnUrl('add_ajax'), {
			
			onSuccess : function(transport) {
				$('add_new_devmod_container').update(transport.responseText);
			}.bind(this)
		
		});
		
	},
	
	addSave : function(form_obj) {

		tinyMCE.triggerSave();
		
		new Ajax.Request(this.returnUrl('add_ajax_save'), {
			
			parameters : $(form_obj).serialize(true),
			
			onLoading : this.showMessage('Neue Daten werden gespeichert.'),
			
			onSuccess : function(transport) {
			
				json = transport.responseText.evalJSON(true);
				
				this.showMessage(json['message']);
				
				if(json['status'] === true) {
				
					Backend.Console('devmod: added new item into devmod table "' + this.devmod_sql_table + '"');
					
					this.start();
					
				}
			
			}.bind(this)
			
		});
		
	},
	
	/* edit method */
	
	edit : function(item_id) {
		
		var edit_elmn = new Element("div", { id : 'edit_' + item_id });
		
		this.active_tab = tabber.addTab('Bearbeiten (' + item_id + ')', edit_elmn);
		
		new Ajax.Request(this.returnUrl('edit_ajax'), {
			
			parameters : { id_param : item_id },
			
			onLoading : this.showMessage('Lade Bearbeiten Formular'),
			
			onSuccess : function(transport) {
			
				edit_elmn.update(transport.responseText);
				
				tabber.drp(this.active_tab);
				
			}.bind(this)
			
		});
		
	},
	
	editSave : function(form_obj) {
		
		tinyMCE.triggerSave();
		
		new Ajax.Request(this.returnUrl('edit_ajax_save'), {
			
			parameters : $(form_obj).serialize(true),
			
			onLoading : this.showMessage('Daten werden gespeichert'),
			
			onSuccess : function(transport) {
			
				json = transport.responseText.evalJSON(true);
				
				this.showMessage(json['message']);
				
				if(json['status'] === true) {
				
					Backend.Console('devmod: edited item from devmod table "' + this.devmod_sql_table + '"');
					
					this.start();
				}	
			
			}.bind(this)
			
		});
	},
	
	/* delete method */
	
	del : function(item_id) {
		
		check = confirm("Wollen Sie diesen Eintrag wirklich entfernen?");
		
		if(check != false)
		{
			
			new Ajax.Request(this.returnUrl('delete_ajax'), {
				
				parameters : { id_param : item_id },
				
				onLoading : this.showMessage('Entfernen wird ausgef&uuml;hrt'),
				
				onSuccess : function(transport) {
					
					json = transport.responseText.evalJSON(true);
					
					this.showMessage(json['message']);
					
					if(json['status'] === true)
					{
						Backend.Console('devmod: deleted item from devmod table "' + this.devmod_sql_table + '"');
						
						this.start();
					}
					
				}.bind(this)
				
			});
		
		}
		
	},
	
	multiDel : function(form_obj) {
		
		check = confirm("Wollen Sie alle markierten Elemente wirklich unwiderruflich entfernen?");
		
		if(check != false)
		{
			new Ajax.Request(this.returnUrl('delete_ajax_multi'), {
				
				parameters : $(form_obj).serialize(true),
				
				onSuccess : function(transport) {
				
					json = transport.responseText.evalJSON(true);
					
					this.showMessage(json['message']);
					
					if(json['status'] === true)
					{
						this.start();
					}
					
				}.bind(this)
			
			});
			
		}
		
	},
	
	multiDelSelect : function()
	{
		$$('.checkbox_selectors').each(function(item){
			if(item.checked == true)
			{
				item.checked = false;
			} else {
				item.checked = true;
			}
		});
	},
	
	/* file uploading */
	
	fileUploadStart : function() {
		
		$('file_loader').show();
		
	},
	
	fileUploadEnd : function(status, message, file_id, file_name, field) {
		
		$('file_loader').hide();
		
		if(status === false)
		{
			alert(message);
		} else {
			$('field_' + field).value = file_id;
			$('file_' + field).update(file_name);
			this.closeAddon('div_' + field);
		}
		
	},
	
	/* image uploading */
	
	imageUploadStart : function() {
		$('image_loader').show();
	},
	
	imageUploadEnd : function(status, message, image_id, image_tag, field) {
		$('image_loader').hide();
		$('field_' + field).value = image_id;
		$('img_' + field).update('<img src="' + image_tag + '" border="0" />');
		this.closeAddon('div_' + field);
	},
	
	/* addon methods */

	openAddon : function(div_id, event) {
		
		$(this.layer_id).setStyle({ 
			'height' : '100%',
			'width' : '100%',
			'z-index' : '50'
		});
		
		 $(div_id).setStyle({ 
			'position' : 'absolute',
			'top'      : (event.clientY-10) + 'px',
			'left'     : (event.clientX-10) + 'px',
			'z-index' : '100'
		});
		
		$(this.layer_id).show();
		$(div_id).show();
		
	},
	
	closeAddon : function(div_id) {
		
		Effect.toggle(div_id, 'appear');
		Effect.toggle(this.layer_id, 'appear');
		
	},
	
	/* list element click buttons/actions */
	
	buttonSearch : function() {
		
		new Ajax.Request(this.returnUrl('list_ajax_search') + '&search_str=' + $F('search_str'), {
			
			onSuccess : function(transport) {
			
				$(this.devmod_container).update(transport.responseText);
			
			}.bind(this)
			
		});
		
	},
	
	buttonExport : function(buttonText) {
		
		var hash = new Hash();
		
		$$('.js_export_item').each(function(item){
			if($(item).checked) {
				hash.set($(item).value, $(item).value);
			}
		});
		
		new Ajax.Request(this.returnUrl('list_ajax_export'), {
			
			parameters : hash.toObject(),
			
			onSuccess : function(transport) {
			
				$('export_link').update('<a class=\'button\' style="color:#333; padding:6px;" target="_blank" href="'+ transport.responseText +'">' + buttonText + '</a>');
			
			}.bind(this)
			
		});
		
	},
	
	/* ADDON FUNCTIONS */
	
	AddonSqlCheckbox : function(form_obj, success_div) {
		
		new Ajax.Request(this.returnUrl('addon_sqlCheckbox'), {
			
			parameters : $(form_obj).serialize(true),
			
			onSuccess : function(transport) {
			
				if(transport.responseText == 1)
				{
					new Effect.Highlight(success_div, { startcolor:'#0f8511', endcolor:'#525E72', restorecolor:'#525E72' });
					Backend.Console('devmod: addon submit response = true; id = ' + success_div);
	
				} else {
					alert('error while saving refs_table!');
				}
			
			}.bind(this)
			
		});
	},
	
	/* private functions */
	
	returnUrl : function(method) {
		
		return Settings.Host + this.callback_url + '&method=' + method + '&check_sum=' + this.checksum;

	},
	
	showMessage : function(message) {
		
		$(this.devmod_loader).update(message);
		
		$(this.devmod_loader).show();
		
		if(this.timeout !== null)
		{
			clearTimeout(this.timeout);
		}
		
		this.timeout = setTimeout("$(" + this.devmod_loader + ").appear({ duration: 0.8, from : 1.0 , to : 0.0 });", 2000);
		
	}
});